﻿using Core.Entities.SM;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Data.Configurations.SM
{
    /// <summary>
    /// 库存台账配置
    /// </summary>
    public class StockConfiguration : IEntityTypeConfiguration<Stock>
    {
        public void Configure(EntityTypeBuilder<Stock> builder)
        {
            // 设置表名
            builder.ToTable("SM_Stock");

            // 配置主键
            builder.HasKey(s => s.Id);

            // 属性配置
            builder.Property(s => s.Id)
                .HasColumnName("ID");

            builder.Property(s => s.MaterCode)
                .HasColumnName("MaterCode")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.MaterName)
                .HasColumnName("MaterName")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.MaterType)
                .HasColumnName("MaterType")
                .HasMaxLength(20)
                .IsRequired(false);

            builder.Property(s => s.BatchCode)
                .HasColumnName("BatchCode")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.WarehouseID)
                .HasColumnName("WarehouseID")  // 注意数据库字段名是 WarehouselD (包含小写L)
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.WarehouseName)
                .HasColumnName("WarehouseName")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.LocationID)
                .HasColumnName("LocationID")
                .HasMaxLength(50)
                .IsRequired(false);

            builder.Property(s => s.LocationName)
                .HasColumnName("LocationName")
                .HasMaxLength(50)
                .IsRequired(false);

            builder.Property(s => s.Quantity)
                .HasColumnName("Quantity")
                .HasColumnType("decimal(18, 2)")
                .IsRequired(false);

            builder.Property(s => s.Type)
                .HasColumnName("Type")
                .HasMaxLength(50)
                .IsRequired(false);

            builder.Property(s => s.Status)
                .HasColumnName("Status")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.Spec)
                .HasColumnName("Spec")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.SiloCode)
                .HasColumnName("SiloCode")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.SiloName)
                .HasColumnName("SiloName")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.CreateTime)
                .HasColumnName("CreateTime")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.OutTime)
                .HasColumnName("OutTime")
                .HasMaxLength(255)
                .IsRequired(false);

            builder.Property(s => s.IsDeleted)
                .HasColumnName("IsDeleted");

            // 索引配置
            builder.HasIndex(s => s.MaterCode)
                .HasDatabaseName("IX_SM_Stock_MaterCode");

            builder.HasIndex(s => s.BatchCode)
                .HasDatabaseName("IX_SM_Stock_BatchCode");

            builder.HasIndex(s => s.WarehouseID)
                .HasDatabaseName("IX_SM_Stock_WarehouseID");

            builder.HasIndex(s => s.LocationID)
                .HasDatabaseName("IX_SM_Stock_LocationID");

            builder.HasIndex(s => s.SiloCode)
                .HasDatabaseName("IX_SM_Stock_SiloCode");

        }
    }
}